package com.taihua_tech.basic.util;

import java.io.Serializable;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

public class DataRow extends DataMap implements Serializable{
	private static final long serialVersionUID = -6999694094130576413L;
	private static HashMap<String,DataRowConvert> GlobalConvertMap = new HashMap<String,DataRowConvert>();
	
	private HashMap<String,DataRowConvert> fieldConvertMap = new HashMap<String,DataRowConvert>(); 
	
	public DataRow() {
		super();
	}
	public DataRow(Object... args) {
		super(args);
	}	

    public Object get(Object key) {
    	if (!this.fieldConvertMap.isEmpty() && this.fieldConvertMap.containsKey(key)){
    		DataRowConvert fieldConvert = fieldConvertMap.get(key);
    		Object value = super.get(key);
    		return fieldConvert.convert(value);
    	}
    	else if (GlobalConvertMap.containsKey(key)){
    		DataRowConvert fieldConvert = GlobalConvertMap.get(key);
    		Object value = super.get(key);
    		return fieldConvert.convert(value);
    	}
    	else{
    		return super.get(key);
    	}
    }
	
    public void addConvert(String key,DataRowConvert fieldConvert){
    	fieldConvertMap.put(key, fieldConvert);
    }
    
    public static void addGlobalConvert(String key,DataRowConvert fieldConvert){
    	GlobalConvertMap.put(key, fieldConvert);
    }
    
	@SuppressWarnings("rawtypes")
	public DataParam toDataParam(){
		DataParam result = new DataParam();
		Iterator iterator = this.keySet().iterator();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			String value = stringValue(key);
			result.put(String.valueOf(key), value);				
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public DataParam toDataParam(boolean keepMetaType){
		if (keepMetaType){
			DataParam result = new DataParam();
			Iterator iterator = this.keySet().iterator();
			while (iterator.hasNext()) {
				Object key = iterator.next();
				Object value = this.get(key);
				result.put(String.valueOf(key), value);				
			}
			return result;
		}else{
			return toDataParam();
		}
	}
	public String toXML(){
		String result = null;
		Document document = XmlUtil.createDocument();
		Element rootElement = document.addElement("row");
		Iterator<String> iter = this.keySet().iterator();
		while(iter.hasNext()){
			String key = iter.next();
			String value = this.stringValue(key);
			rootElement.addElement(key).setText(value);
		}
		result = document.asXML();
		return result;
	}
	@SuppressWarnings("rawtypes")
	public void initFromXML(String xml){
		Document document = XmlUtil.readDocument(new StringReader(xml));
    	String rowPath = "//row";
    	Node node = document.selectSingleNode(rowPath);
		Element rowElement = (Element)node;
		List eList = rowElement.elements();
		for (int i=0;i < eList.size();i++){
			Element field = (Element)eList.get(i);
			String key = field.getName();
			String value = field.getText();
			this.put(key,value);
		}
	}
	public String toString(){
		return super.toString();
	}
}